package logic

import (
	"context"
	"go-exp/pkg/ctxdata"
	"go-exp/pkg/errorx"
	"go-exp/query"
	"go-exp/zero/rpc/user/internal/svc"
	"go-exp/zero/rpc/user/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取用户详情
func (l *GetUserLogic) GetUser(in *user.GetUserRequest) (*user.User, error) {
	// todo: add your logic here and delete this line
	u := query.SysUser
	first, _ := u.WithContext(l.ctx).Where(u.ID.Eq(ctxdata.GetUserID(l.ctx))).First()
	logx.Infof("u: %v", u)
	if first == nil {
		return nil, errorx.NewCodeError(500, "用户不存在")
	}
	return &user.User{
		Id:        first.ID,
		Username:  first.Username,
		Password:  first.PasswordHash,
		CreatedAt: first.CreatedAt.Format("2006-01-02 15:04:05"),
	}, nil
}
